package cn.droidlover.xdroidmvp.mvp;

import java.util.ArrayList;

import android.app.Activity;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Activity管理类
 * @author Egoist
 */
public final class XManager {


	private ArrayList<XActivity> activityList = new ArrayList<XActivity>();
	/**
	 * 单例对象
	 */
	private static XManager instance;

	/**
	 * 私有构造
	 */
	private XManager() {

	}

	/**
	 * 单例模式中获取唯一的ExitApplication实例
	 * @return ExitApplication实例
	 */
	public static XManager getInstance() {
		if (null == instance) {
			instance = new XManager();
		}
		return instance;

	}

	/**
	 * 添加Activity到容器中
	 * @param activity
	 */
	public void addActivity(XActivity activity) {
		if (activityList == null) {
			return;
		}
		synchronized (activityList) {
			if (!activityList.contains(activity)) {
				activityList.add(activity);
			}
		}
	}

	/**
	 * 删除Activity到容器中
	 * @param activity
	 */
	public void delActivity(Activity activity) {
		if (activityList == null) {
			return;
		}
		synchronized (activityList) {
			if (activityList.contains(activity)) {
				activityList.remove(activity);
			}
		}
	}

	/**
	 * 杀客户端进程
	 */
	public void killAppStoreProcess() {
		XManager.getInstance().exit();
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	/**
	 * 遍历所有Activity并finish
	 */
	public void exit() {
		if (activityList == null) {
			return;
		}
		Activity act = null;
		for (int i = 0; i < activityList.size(); i++) {
			act = activityList.get(i);
			if (act != null) {
				act.finish();
			}
		}
	}


}